package com.echronos.workflow.test;

import org.apache.commons.io.IOUtils;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.repository.ProcessDefinitionQuery;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class QueryBpmnFile {
    public static void main(String[] args) {
        //创建流程引擎配置类
        ProcessEngineConfiguration processEngineConfiguration=
                ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml");
        //流程引擎对象
        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        //获取查询器
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        //设置查询条件
        processDefinitionQuery.processDefinitionKey("myProcess");

        //执行查询操作，得到想要流程定义信息
        ProcessDefinition processDefinition = processDefinitionQuery.singleResult();

        //通过流程定义信息得到部署id
        String deploymentId = processDefinition.getDeploymentId();

        //实现读写bpmn文件信息
        InputStream bpmnIs = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName());

        //构建输出流
        FileOutputStream bpmnOs = null;
        try {
            bpmnOs = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\新建文件夹\\"+processDefinition.getResourceName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        //输入流，输出流的转换
        try {
            IOUtils.copy(bpmnIs,bpmnOs);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}